<template>
  <div id="home">
    <!--    导航-->
    <nav-bar class="home-nav">
      <div slot="center">购物街888</div>
    </nav-bar>

    <!--  选项卡  -->
    <tab-control @tabControlChange="tabControlChangeHandler"
                 :titles="['流行','新款','精选']"
                 ref="tabControl1"
                 class="tab-control"
                 v-show="isShowfakeTabControl"
                 :currentIndex="currentIndex"></tab-control>

    <scroll :probeType="3"
            :pullUpLoad="true"
            @pullingUp="loadMore"
            @scroll="scrollHandler"
            ref="scroll"
            class="wrapper">
      <!--    轮播-->
      <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoadHandler"></home-swiper>
      <!--    推荐-->
      <recommend-view :recommends="recommends"></recommend-view>
      <!--    本周流行-->
      <feature-view :features="features"></feature-view>
      <!--  选项卡  -->
      <tab-control @tabControlChange="tabControlChangeHandler"
                   :titles="['流行','新款','精选']"
                   ref="tabControl2"
                   :currentIndex="currentIndex"></tab-control>
      <!--    商品列表-->
      <goods-list :goods="showGoods"></goods-list>
    </scroll>


    <back-top v-show="isShowBackTop" @click.native="backClick"></back-top>


  </div>
</template>

<script>

  import HomeSwiper from "@/views/home/childComps/HomeSwiper"
  import RecommendView from "@/views/home/childComps/RecommendView"
  import FeatureView from "@/views/home/childComps/FeatureView"

  import NavBar from "@/components/common/navbar/NavBar"
  import TabControl from "@/components/content/tabControl/TabControl";
  import GoodsList from "@/components/content/goods/GoodsList";
  import Scroll from "@/components/common/scroll/Scroll";
  import BackTop from "@/components/content/backTap/BackTop";


  import {getHomeMultidata, getHomeGoods} from "@/network/home";


  export default {
    name: "Home",
    data() {
      return {
        banners: [],
        recommends: [],
        features: [],
        goods: {
          //流行
          pop: {page: 0, list: []},
          //新款
          new: {page: 0, list: []},
          //精选
          sell: {page: 0, list: []}
        },
        currentType: "pop",
        isShowBackTop: false,
        tabControlOffsetTop: 0,
        isShowfakeTabControl: false,
        currentIndex: 0

      }
    },
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    mounted() {
      this.getHomeMultidata();

      this.getAllHomeGoods();


    },


    methods: {
      /**
       *网络请求相关方法
       *
       */

      //1.  请求首页多个数据
      async getHomeMultidata() {
        const res = await getHomeMultidata();
        this.banners = res.data.banner.list;

        this.recommends = res.data.recommend.list;

        this.features = this.mockFeaturesData();


      },

      // 2. 请求首页某个商品的下一页商品数据
      async getHomeGoods(type) {
        //当前page+1得到下一页的页码
        const page = this.goods[type].page += 1;
        //请求下一页数据
        const res = await getHomeGoods(type, page);
        //把请求过来的数据和原数据做合并
        this.goods[type].list = [...this.goods[type].list, ...res.data.list];
      },

      // 2-plus 拿到首页三种类型商品数据
      getAllHomeGoods() {
        this.getHomeGoods("pop");
        this.getHomeGoods("new");
        this.getHomeGoods("sell");
      },

      //自己模拟features的数据
      mockFeaturesData() {
        return [
          {
            image: require("@/assets/img/home/feature/01.png"),
            title: "清新笔记本"
          },
          {
            image: require("@/assets/img/home/feature/02.png"),
            title: "双色靠座椅"
          },
          {
            image: require("@/assets/img/home/feature/03.png"),
            title: "黑羊绒毛衣"
          },
          {
            image: require("@/assets/img/home/feature/04.png"),
            title: "七彩针织衫"
          },
          {
            image: require("@/assets/img/home/feature/05.png"),
            title: "抹茶橡皮擦"
          },
          {
            image: require("@/assets/img/home/feature/06.png"),
            title: "微胖遮肉服"
          },
          {
            image: require("@/assets/img/home/feature/07.png"),
            title: "青峰瓷大碗"
          },
          {
            image: require("@/assets/img/home/feature/08.png"),
            title: "高能太阳镜"
          }
        ];
      },


      /**
       *事件监听相关方法
       *
       */
      //tabControl选项卡切换时修改填充的商品数据
      tabControlChangeHandler(index) {
        let mapping = {
          "0": "pop",
          "1": "new",
          "2": "sell"
        };
        this.currentType = mapping[index];
        this.currentIndex = index;

        //切换到另外选项卡的时候让商品展示位置变为第一部分
        this.$refs.scroll.bscroll.scrollTo(0, -this.tabControlOffsetTop, 0);
      },


      scrollHandler(position) {
        //判断backtop是否显示
        if (-position.y > 1500) {
          this.isShowBackTop = true;
        } else {
          this.isShowBackTop = false;
        }

        //判断tabControl是否吸顶（position：fixed）
        if (-position.y > this.tabControlOffsetTop) {
          this.isShowfakeTabControl = true;
        } else {
          this.isShowfakeTabControl = false;
        }

      },

      //backtop返回顶部
      backClick() {
        this.$refs.scroll.scrollTo(0, 0);
      },


      //触底之后加载下一页数据
      loadMore() {
        this.getHomeGoods(this.currentType);
      },

      swiperImgLoadHandler() {
        //获取tabControl的offsetTo
        //所有的组件都有个$el属性；用于获取组件中的元素
        this.tabControlOffsetTop = this.$refs.tabControl2.$el.offsetTop;
        // console.log(this.tabControlOffsetTop);


      }

    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list;
      }
    }


  }
</script>

<style scoped>

  #home {
    /*视口*/
    height: 100vh;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;


  }


  .wrapper {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: hidden;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }


</style>
